<?php
class IndexIncludeServer {
	//新闻发布型的用模块连接
	//其他链接一律采取链接格式
	public function  getTop($url_info){
		if(!empty($url_info)){
			$id = $url_info['id'];
			$father_id = $url_info['father_id'];
		}
		$navDao  = HttpDaoManager::getNavDao();
		$data['nav'] = $navDao->searchList(array('father_id'=>FRONT_INDEX_NAV,'status'=>NAV_STATUS_1)," order by sort asc");
		foreach($data['nav'] as $key=>$value){
			$data['child_nav'][$value['id']] = $navDao->searchList(array('father_id'=>$value['id'],'status'=>NAV_STATUS_1)," order by sort asc");
			
		}
		$control = isset($_GET['control']) ? $_GET['control'] : INDEX_FLAG;
		if(!isset($father_id)){
			return $data;
		}
		if($father_id==FRONT_PARENT_NAV){//若是首页导航、会员导航则不进行显示
			$father_id = $id;
		}else{
				if($father_id==FRONT_INDEX_NAV){
					$father_id = $id;
				}
				foreach($data['nav'] as $key=>$value){
					$data['nav'][$key]['active']='off';
					if($value['id']==$father_id){
						$data['nav'][$key]['active']='on';
					}
				}
		}
		return $data;
	}
	
	public function getFoot(){
		$courseList = HttpDaoManager::getCourseTypeDao()->searchList(array('status'=>COURSE_TYPE_STATUS_2));
		$course_type = Common::_t('course_type');
		$course_type_list = array();
		foreach($courseList  as $key=>$value){
			$course_type_list[$value['type']][$value['course_type']]['id']= $value['id'];
			$course_type_list[$value['type']][$value['course_type']]['course_type']= $value['course_type'];
			$course_type_list[$value['type']][$value['course_type']]['name']= $course_type[$value['course_type']];
		}
		$data['course_type_list'] = $course_type_list;
		return  $data;
	}
	
	public function getUserTop(){
		$data['count'] = HttpDaoManager::getLetterStationDao()->count(array('user_id'=>$_SESSION['user']['id'],'status'=>STATION_STATUS_0));
		$data['nav']  = HttpDaoManager::getNavDao()->searchList(array('father_id'=>FRONT_INDEX_NAV,'status'=>NAV_STATUS_1)," order by sort asc");
		foreach($data['nav'] as $key=>$value){
			$data['child_nav'][$value['id']] = HttpDaoManager::getNavDao()->searchList(array('father_id'=>$value['id'],'status'=>NAV_STATUS_1)," order by sort asc");
		}
		return  $data;
	}
	public function getFoots(){
		$data  = array();
		return $data;
	}
	public function getUserFoot(){
		return  $this->getFoots();
	}
	public function getBanner($url_info){
		$tmp = array();
		$navDao  = HttpDaoManager::getNavDao();
		if ($url_info['father_id']!=FRONT_INDEX_NAV){
			if (empty($url_info['banner'])){
				$tmp = $navDao->get($url_info['father_id']);
			}
			else{
				$tmp['banner'] = $url_info['banner'];
			}
		}else{
			$tmp['banner'] = $url_info['banner'];
		}
		
		return  $tmp;
	}

	public function getLeft($url_info){
		$data = array();
		$id = $url_info['id'];
		if($id!=FRONT_INDEX_NAV){//首页导航
			$data = $this->getIndexLeft($id);
			if($data['father_nav']['id']==$id){
				$data['click_news'] = 'index';
			}else{
				$data['click_news'] = $id;
			}
			$where = array();
			$where['nav_id'] = 79;
			$where['type'] = NEWS_PUB_TYPE_LIST;
			$order_by = ' order by recom_status desc,id desc ';
			$data['news_list'] = HttpServerManager::getNewsServer()->paging($where,$order_by,5);
		}
		return $data;		
	}
	
	public function getUserLeft($left_url=""){
		$url = $this->getUrl();
		if(!empty($left_url)){
			$url = $left_url;
		}
		$navDao  = HttpDaoManager::getNavDao();
		$nav = $navDao->getByUrlAndFatherId(trim($url),FRONT_USER_NAV);
		$data['navs'] = HttpDaoManager::getNavDao()->getListByFatherId(FRONT_USER_NAV);
		
		if(empty($nav)){//二级目录
			$nav = $navDao->getByUrlAndChildId(trim($url),FRONT_USER_NAV);//当前路径
			if(!empty($nav)){
				$father_id  = $nav['father_id'];
				$father_nav = $navDao->get($father_id);//当前路径
				$url = $father_nav['url'];
			}
		}
		$hover = false;
		foreach($data['navs'] as $key=>$value){
			$data['navs'][$key]['active'] = 'off';
			if(stripos($url,$value['url'])>-1){
				$data['navs'][$key]['active'] = 'on';
				$hover = true;
			}
		}
		
		$data['hover'] = $hover;
		$where = array();
		$where['nav_id'] = 79;
		$where['type'] = NEWS_PUB_TYPE_LIST;
		$order_by = ' order by recom_status desc,id desc ';
		$data['news_list'] = HttpServerManager::getNewsServer()->paging($where,$order_by,5);		
		return  $data;
	}
	
	public function getUserMenu(){
		$data  = array();
		$url = $this->getFullUrl();
		$navDao  = HttpDaoManager::getNavDao();
		$nav_url = $navDao->getByUrlAndChildId(trim($url),FRONT_USER_NAV);//当前路径
		if(empty($nav_url)){
			$url = $this->getUrl();
			$nav_url = $navDao->getByUrlAndChildId(trim($url),FRONT_USER_NAV);//当前路径
		}
		if(empty($nav_url)){
			$nav_url = $navDao->getByUrlAndFatherId(trim($url),FRONT_USER_NAV);
			if(!empty($nav_url)){
				$father_id = $nav_url['id'];
			}
		}else{
			$father_id  = $nav_url['father_id'];
		}
		if(empty($father_id)){
			return $data;
		}
		$data['navs'] = HttpDaoManager::getNavDao()->getListByFatherId($father_id);
		$hover = false;
		foreach($data['navs'] as $key=>$value){
			$data['navs'][$key]['active'] = 'off';
			if(stripos($value['url'],$url)>-1){
				$data['navs'][$key]['active'] = 'on';
				$hover = true;
			}
		}	
		$data['hover'] = $hover;	
		return  $data;
	}
	//可以设置默认父亲节点
	public function getPublic($url=null){
		if($url==null){
			$url = $this->getUrl();
		}
		$navDao = HttpDaoManager::getNavDao();
		$url_info = $navDao->getByUrl($url);
		if(empty($url_info)){//证明该系统为模块URL,原则不允许一个模块即为静态URL和模块 URL并存
			if(!empty($_GET['pubflg'])){//列表发布情况
				$id = $_GET['id'];
				$newsDao = HttpDaoManager::getNewsDao();
				$news_info = $newsDao->get($id);
				$nav_id = $news_info['nav_id'];
				$id = $nav_id;				
				$url_info = $navDao->get($id);
			}else{//静态发布情况
				if(isset($_GET['id'])&&intval($_GET['id']>0)){
					$url_info = $navDao->getByControl($_GET['control'],$id);
				}
			}
			//其他链接
			if(empty($url_info)){
				$id = FRONT_INDEX_NAV;
				$url_info = $navDao->get($id);
			}
		}
		
		$data['top'] = $this->getTop($url_info);
		$data['banner'] = $this->getBanner($url_info);
		$data['left'] = $this->getLeft($url_info);
		$data['foot']  = $this->getFoot();

		return $data;
	}
	
	public function getIndexLeft($id){
		$navDao  = HttpDaoManager::getNavDao();
		$data['father_nav'] = $navDao->get($id);
		$father_id  = $data['father_nav']['father_id'];
		if($father_id==FRONT_INDEX_NAV){//若父亲本身就是一级菜单
			$data['child_nav'] = $navDao->getListByFatherId($id);
		}else{//不是的话，则继续向上
			unset($data);
			$data = $this->getIndexLeft($father_id);
		}
		return  $data;
	}
	
	public function getUrl(){
		$request_uri = $_SERVER['REQUEST_URI'];
		$request_uri_array = explode("/",$request_uri);
		$url="";		
		foreach($request_uri_array as $key=>$value){
			if(stripos($value,'.html')>-1||stripos($value,'.htm')>-1||stripos($value,'.php')>-1){
				$url = $value;
				if(stripos($value,'?')){
					$pos = stripos($value,'?');
					$url = substr($value, 0,$pos);
				}
			}
		}
		return $url;
	}
	
	public function getFullUrl(){
		$request_uri = $_SERVER['REQUEST_URI'];
		$request_uri_array = explode("/",$request_uri);
		$url="";		
		foreach($request_uri_array as $key=>$value){
			if(stripos($value,'.html')>-1||stripos($value,'.htm')>-1||stripos($value,'.php')>-1){
				$url = $value;
			}
		}
		return $url;
	}
}
?>